package edu.rmit.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import edu.rmit.domain.User;
import edu.rmit.service.IUserService;
import edu.rmit.util.PromotDialog;

/**
 * This panel is used to input message on the live chat and forum window.
 * 
 * @author Joey George
 * @version 1.0
 */
@SuppressWarnings("serial")
public class RegisterPanel extends JPanel {
	/**
	 * The frame object to initialize the window.
	 */
	private JFrame frame;

	/**
	 * The window initialize height.
	 */
	private static int HEIGHT = 250;

	/**
	 * The window initialize width.
	 */
	private static int WIDTH = 400;

	/**
	 * The label which display the user name.
	 */
	private JLabel usernameLabel;

	/**
	 * The label which display the password
	 */
	private JLabel passwordLabel;

	/**
	 * The label which display the password again.
	 */
	private JLabel passwordAgainLabel;

	/**
	 * The label which display the first name.
	 */
	private JLabel firstNameLabel;

	/**
	 * The label which display the last name.
	 */
	private JLabel lastNameLabel;

	/**
	 * The text field which display the user name.
	 */
	private JTextField usernameField;

	/**
	 * The password field which display the password.
	 */
	private JPasswordField passwordField;

	/**
	 * The password field which display the password again.
	 */
	private JPasswordField passwordAgainField;

	/**
	 * The first name field which display the first name.
	 */
	private JTextField firstNameField;

	/**
	 * The last name field which display the last name.
	 */
	private JTextField lastNameField;

	/**
	 * The register button which clicked will cause a user to register.
	 */
	private JButton registerButton;

	/**
	 * The cancel button which clicked will cancel the register.
	 */
	private JButton cancelButton;

	/**
	 * The client instance can get the service which the RMI service provide.
	 */
	private Client client;

	/**
	 * The constructor to initialize the register panel and some other
	 * corresponding control.
	 * 
	 * @param client
	 *            The client instance which can obtain service provide by RMI
	 *            server.
	 */
	public RegisterPanel(Client client) {
		this.client = client;

		usernameLabel = new JLabel("username:");
		passwordLabel = new JLabel("password:");
		passwordAgainLabel = new JLabel("pass again:");
		firstNameLabel = new JLabel("firstName:");
		lastNameLabel = new JLabel("lastName:");

		usernameField = new JTextField(10);
		passwordField = new JPasswordField(10);
		passwordAgainField = new JPasswordField(10);
		firstNameField = new JTextField(10);
		lastNameField = new JTextField(10);

		registerButton = new JButton("Register");
		cancelButton = new JButton("Cancel");

		registerButton.addActionListener(new ListenerRegisterComfirm());
		cancelButton.addActionListener(new ListenerCancelRegister());

		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new GridLayout(5, 1));

		JPanel usernamePanel = new JPanel();
		usernamePanel.add(usernameLabel);
		usernamePanel.add(usernameField);

		JPanel passwordPanel = new JPanel();
		passwordPanel.add(passwordLabel);
		passwordPanel.add(passwordField);

		JPanel passwordAgainPanel = new JPanel();
		passwordAgainPanel.add(passwordAgainLabel);
		passwordAgainPanel.add(passwordAgainField);

		JPanel firstNamePanel = new JPanel();
		firstNamePanel.add(firstNameLabel);
		firstNamePanel.add(firstNameField);

		JPanel lastNamePanel = new JPanel();
		lastNamePanel.add(lastNameLabel);
		lastNamePanel.add(lastNameField);

		JPanel registerPanel = new JPanel();
		registerPanel.setLayout(new GridLayout(5, 1));
		registerPanel.add(new JLabel());
		registerPanel.add(registerButton);
		registerPanel.add(new JLabel());
		registerPanel.add(cancelButton);

		JPanel leftPanel = new JPanel();
		leftPanel.add(contentPanel);

		JPanel rightPanel = new JPanel();
		rightPanel.add(registerPanel);

		JPanel resultPanel = new JPanel();
		resultPanel.setLayout(new BorderLayout());

		resultPanel.add(leftPanel, BorderLayout.WEST);
		resultPanel.add(rightPanel, BorderLayout.CENTER);

		contentPanel.add(usernamePanel);
		contentPanel.add(passwordPanel);
		contentPanel.add(passwordAgainPanel);
		contentPanel.add(firstNamePanel);
		contentPanel.add(lastNamePanel);

		frame = new JFrame("User Register Window");
		frame.setContentPane(resultPanel);
		initFrame();
	}

	/**
	 * This method is to initialize the window and the corresponding control.
	 */
	private void initFrame() {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setSize(WIDTH, HEIGHT);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
	}

	/**
	 * This listener class is to implement the logic when the user click the
	 * register button.
	 * 
	 * @author Joey George
	 * @version 1.0
	 */
	class ListenerRegisterComfirm implements ActionListener {

		/**
		 * This method is to implement the logic when a user clicked the
		 * register button.
		 */
		@Override
		public void actionPerformed(ActionEvent event) {
			String username = usernameField.getText().trim();
			String password = new String(passwordField.getPassword());
			String passAgain = new String(passwordAgainField.getPassword());
			String firstName = firstNameField.getText().trim();
			String lastName = lastNameField.getText().trim();

			boolean isSuccessful = checkParameter(username, password, passAgain);
			if (isSuccessful) {
				IUserService userService = client.getUserService();
				String title = "Register Error";
				String reason = "";
				try {
					User user = userService.getUser(username);
					if (user != null) {
						reason = "User is existed,please choose another one!";
						PromotDialog.errorMessageBox(null, title, reason);
					} else {
						userService.register(username, password, firstName,
								lastName);
						title = "Register Successful";
						reason = "You have successfully registed!";
						frame.dispose();
						new WelcomePanel(client);
						PromotDialog.successMessageBox(null, title, reason);
					}
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}

		/**
		 * This method is to check the parameter which the user provided
		 * 
		 * @param username
		 *            The user name for register.
		 * @param password
		 *            The password provided for register.
		 * @param passAgain
		 *            The password again provided for register.
		 * @return
		 */
		private boolean checkParameter(String username, String password,
				String passAgain) {
			String reason = "";
			String title = "Parameter Error!";
			if (username.equals("")) {
				reason = "User Name is required!";
				PromotDialog.errorMessageBox(null, title, reason);
				return false;
			} else if (password.equals("")) {
				reason = "Password is required!";
				PromotDialog.errorMessageBox(null, title, reason);
				return false;
			} else if (passAgain.equals("")) {
				reason = "Password again is required!";
				PromotDialog.errorMessageBox(null, title, reason);
				return false;
			} else if (!password.equals(passAgain)) {
				reason = "Password and password again is not equal!";
				PromotDialog.errorMessageBox(null, title, reason);
				return false;
			}
			return true;
		}
	}

	/**
	 * This method is to implement the login when a user clicked the cancel
	 * button.
	 * 
	 * @author Joey George
	 * @version 1.0
	 * 
	 */
	class ListenerCancelRegister implements ActionListener {
		/**
		 * This method is to implement the logic when a user clicked the cancel
		 * button.
		 */
		public void actionPerformed(ActionEvent event) {
			frame.dispose();
			new WelcomePanel(client);
		}
	}
}
